package com.jinzhi.jzapi.certificate.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.service.DictService;
import com.jinzhi.common.utils.ApiResult;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzapi.utils.CommonUtil;
import com.jinzhi.jzweb.domain.InformationImportUserDO;
import com.jinzhi.jzweb.domain.cond.InformationImportUserCond;
import com.jinzhi.jzweb.domain.vo.InformationImportUserVo;
import com.jinzhi.jzweb.service.CertificateInfoService;
import com.jinzhi.jzweb.service.InformationImportUserService;
import com.jinzhi.jzweb.utils.ValuesUtil;
import com.jinzhi.sys.domain.UserDO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

import static com.jinzhi.api.util.TokenTag.TOKEN_TAG;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2024-05-15 17:22:11 | lance</small>
 */
@Controller
@RequestMapping("/jzApi/informationImportUser")
public class ApiInformationImportUserController extends BaseController {
	@Autowired
	private InformationImportUserService informationImportUserService;
	@Autowired
	private DictService dictService;
	@Autowired
	private CertificateInfoService certificateInfoService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:informationImportUser:informationImportUser")
	String InformationImportUser(){
	    return "jzweb/informationImportUser/informationImportUser";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:informationImportUser:informationImportUser")
	public Result<Page<InformationImportUserDO>> list(InformationImportUserDO informationImportUserDTO){
		// 查询列表数据
		Page<InformationImportUserDO> page = informationImportUserService.selectPage(getPage(InformationImportUserDO.class),
					informationImportUserService.convertToEntityWrapper().orderBy("id", false));
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
				InformationImportUserDO obj = (InformationImportUserDO) page.getRecords().get(i);
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:informationImportUser:add")
	String add(){
	    return "jzweb/informationImportUser/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:informationImportUser:edit")
	String edit(@PathVariable("id") Long id,Model model){
		InformationImportUserDO informationImportUser = informationImportUserService.selectById(id);
		model.addAttribute("informationImportUser", informationImportUser);
	    return "jzweb/informationImportUser/edit";
	}
	
	@Log("添加")
	@ResponseBody
	@PostMapping("/save")
//	@RequiresPermissions("jzweb:informationImportUser:add")
	public Result<String> save( InformationImportUserDO informationImportUser){
		informationImportUserService.insert(informationImportUser);
        return Result.ok();
	}
	
	@Log("修改")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:informationImportUser:edit")
	public Result<String>  update( InformationImportUserDO informationImportUser){
		boolean update = informationImportUserService.updateById(informationImportUser);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:informationImportUser:remove")
	public Result<String>  remove( Long id){
		informationImportUserService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:informationImportUser:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		informationImportUserService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}

	/**
	 * 添加-微信小程序用户信息
	 * @return
	 */
	@ApiOperation(value = "添加-微信小程序用户信息")
	@PostMapping(value = "/saveUserInfo")
	@ResponseBody
	@Transactional
	@RequiresAuthentication
	@ApiImplicitParams({@ApiImplicitParam(name = TOKEN_TAG, value = TOKEN_TAG, paramType = "header")})
	public ApiResult<?> saveUserInfo(@RequestHeader(value = TOKEN_TAG, required = false) String token,
											   @RequestBody InformationImportUserCond cond){
		//检查电话
//		boolean b1 = CommonUtil.checkTel(cond.getMobile());
//		if (!b1){
//			return ApiResult.fail("手机号码不正确	");
//		}
		//检查身份证
		boolean validID = CommonUtil.isValidID(cond.getIdcardNumber());
		if (!validID){
			return ApiResult.fail("身份证号码不正确");
		}
		System.out.println(cond.getMobile());
		//获取用户信息
		UserDO user = this.getClientUser(token);

		return informationImportUserService.saveSignUserInfo(user,cond);
	}

	/**
	 * 编辑-微信小程序用户信息
	 * @return
	 */
	@ApiOperation(value = "编辑-微信小程序用户信息")
	@PostMapping(value = "/updateUserInfo")
	@ResponseBody
	@Transactional
	@RequiresAuthentication
	@ApiImplicitParams({@ApiImplicitParam(name = TOKEN_TAG, value = TOKEN_TAG, paramType = "header")})
	public ApiResult<?> updateUserInfo(@RequestHeader(value = TOKEN_TAG, required = false) String token,
											   @RequestBody InformationImportUserCond cond){
		//检查电话
//		boolean b1 = CommonUtil.checkTel(cond.getMobile());
//		if (!b1){
//			return ApiResult.fail("手机号码不正确	");
//		}
		//检查身份证
		boolean validID = CommonUtil.isValidID(cond.getIdcardNumber());
		if (!validID){
			return ApiResult.fail("身份证号码不正确");
		}

		System.out.println(cond.getMobile());
		//获取用户信息
		UserDO user = this.getClientUser(token);
		//获取用户信息

		return informationImportUserService.updateImportUser(cond,user);
	}

	/**
	 * 编辑-微信小程序用户信息
	 * @return
	 */
	@ApiOperation(value = "编辑接口要的用户信息")
	@GetMapping(value = "/getUserInfo")
	@ResponseBody
	@Transactional
	@RequiresAuthentication
	@ApiImplicitParams({@ApiImplicitParam(name = TOKEN_TAG, value = TOKEN_TAG, paramType = "header")})
	public ApiResult<List<InformationImportUserVo>> getUserInfo(@RequestHeader(value = TOKEN_TAG, required = false) String token,
																@RequestParam(value = "declaredCareers", required = false)String declaredCareers){
		//获取用户信息
		UserDO user = this.getClientUser(token);
		List<InformationImportUserVo> voList=informationImportUserService.getInfos(user,null,declaredCareers);
		return ApiResult.success(voList);
	}


	@ApiOperation(value = "个人信息用户审核详情")
	@GetMapping(value = "/getInfo")
	@ResponseBody
	@Transactional
	@RequiresAuthentication
	@ApiImplicitParams({@ApiImplicitParam(name = TOKEN_TAG, value = TOKEN_TAG, paramType = "header")})
	public ApiResult<List<InformationImportUserVo>> getInfo(@RequestHeader(value = TOKEN_TAG, required = false) String token, @RequestParam(value = "checkStatus",defaultValue = "0") Integer checkStatus){
		//获取用户信息
		UserDO user = this.getClientUser(token);
		List<InformationImportUserVo> voList=informationImportUserService.getInfos(user,checkStatus, "");
		return ApiResult.success(voList);
	}



	/**
	 * api小程序端-证书生成pdf
	 *
	 * @Description: 根据用户id获取的信息来生成pdf
	 * @Param: [certificateNo]
	 * @return: com.jinzhi.common.utils.ApiResult<?>
	 * @Author: kl
	 * @Date:
	 */
	@GetMapping("/savePdf")
	@Log("api小程序端-生成pdf")
	@ApiOperation(value = "api小程序端-生成pdf", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@Transactional
	@RequiresAuthentication
	@ApiImplicitParams({@ApiImplicitParam(name = TOKEN_TAG, value = TOKEN_TAG, paramType = "header")})
	@ResponseBody
	public ApiResult<?> savePdf(@RequestHeader(value = TOKEN_TAG, required = false) String token, @RequestParam(value = "id") Long id) {
		//第一步--查询模板
		String traSynthesis = dictService.selDicBy(selThetemplate());
		if (StringUtils.isNotBlank(traSynthesis)) {
			traSynthesis = ValuesUtil.localUrl + traSynthesis;
		}
//			String traSynthesis = "D:/upload/kaoshenshenbaobiao1.jpg";
		//获取用户信息
		UserDO user = this.getClientUser(token);
		System.out.println("模板地址为++++++++++++++++++++++++++++++++++++++++++++++++++++"+traSynthesis);

		//第二步--判断是否有电子公章，在字段里的值获取
//		String electronic = getElectronic();
		String electronic = "";
//		System.out.println("电子寸照地址为++++++++++++++++++++++++++++++++++++++++++++++++++++"+electronic);

		String url = informationImportUserService.savePdf(id, traSynthesis, electronic);

		return ApiResult.success(url);

	}

	//获取电子公章
	private String getElectronic(){
		String electronicSeal = null;
		//获取电子公章
		Map map = new HashMap();
		map.put("name", "金职电子公章");
		map.put("type", "electronic_seal");
		electronicSeal = dictService.selDicBy(map);
		if (StringUtils.isNotBlank(electronicSeal)) {
			electronicSeal = ValuesUtil.localUrl + electronicSeal;
		}
		return electronicSeal;
	}
	private Map selThetemplate() {
		Map map = new HashMap();
		map.put("name", "湖南省职业技能等级认定个人申报");
		map.put("type", "kaoshenshenbaobiao1");
		return map;
	}

}
